﻿using CastingWellEquipmentGUI.Config;
using CastingWellEquipmentGUI.Model;
using CastingWellEquipmentGUI.Utils;
using CastingWellEquipmentGUI.ViewModel.Common.Command;
using CastingWellEquipmentGUI.ViewModel.Common.LogTool;
using CastingWellEquipmentGUI.ViewModel.Common.WindowsManager;
using Confluent.Kafka;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CastingWellEquipmentGUI.ViewModel.Pages
{
    public class DegassingTankPageViewModel : ViewModelBase
    {
        #region 变量/常量
        /// <summary>
        /// 当前窗体名称
        /// </summary>
        private const string showPageTitle = "DegassingTankPage";
        #endregion 变量/常量

        #region 页面属性
        #region 加热器电流
        /// <summary>
        /// 加热器电流PV1
        /// </summary>
        public string Label_HeaterCurrent_PV1
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_HeaterCurrent_PV1"); }
        }
        /// <summary>
        /// 加热器电流PV2
        /// </summary>
        public string Label_HeaterCurrent_PV2
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_HeaterCurrent_PV2"); }
        }
        /// <summary>
        /// 加热器电流PV3
        /// </summary>
        public string Label_HeaterCurrent_PV3
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_HeaterCurrent_PV3"); }
        }
        /// <summary>
        /// 加热器电流PV4
        /// </summary>
        public string Label_HeaterCurrent_PV4
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_HeaterCurrent_PV4"); }
        }
        /// <summary>
        /// 加热器电流PV5
        /// </summary>
        public string Label_HeaterCurrent_PV5
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_HeaterCurrent_PV5"); }
        }
        /// <summary>
        /// 加热器电流PV6
        /// </summary>
        public string Label_HeaterCurrent_PV6
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_HeaterCurrent_PV6"); }
        }
        #endregion 加热器电流

        #region 温度
        /// <summary>
        /// 温度PV1
        /// </summary>
        public string Label_Temperature_PV1
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_Temperature_PV1"); }
        }
        /// <summary>
        /// 温度PV2
        /// </summary>
        public string Label_Temperature_PV2
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_Temperature_PV2"); }
        }
        /// <summary>
        /// 温度PV3
        /// </summary>
        public string Label_Temperature_PV3
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_Temperature_PV3"); }
        }
        /// <summary>
        /// 温度PV4
        /// </summary>
        public string Label_Temperature_PV4
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_Temperature_PV4"); }
        }
        private string _Button_Temperature_SP;
        /// <summary>
        /// 温度SP
        /// </summary>
        public string Button_Temperature_SP
        {
            get { return _Button_Temperature_SP; }
            set
            {
                if (KafkaHelper.SendGatewayChangeTagMessage(showPageTitle, "Button_Temperature_SP", value))
                {
                    _Button_Temperature_SP = value;
                    RaisePropertyChanged("Button_TiltingRateOfHoldingFurnaceCV");
                    RaisePropertyChanged("Label_Temperature_PV1");
                    RaisePropertyChanged("Label_Temperature_PV2");
                    RaisePropertyChanged("Label_Temperature_PV3");
                    RaisePropertyChanged("Label_Temperature_PV4");
                    RaisePropertyChanged("Button_Temperature_SP");
                }
            }
        }
        #endregion 温度

        #region 转子负载
        /// <summary>
        /// 转子负载PV1
        /// </summary>
        public string Label_RotorLoad_PV1
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_RotorLoad_PV1"); }
        }
        /// <summary>
        /// 转子负载PV2
        /// </summary>
        public string Label_RotorLoad_PV2
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_RotorLoad_PV2"); }
        }
        /// <summary>
        /// 转子负载PV3
        /// </summary>
        public string Label_RotorLoad_PV3
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_RotorLoad_PV3"); }
        }

        #endregion 转子负载

        #region 转子转速
        /// <summary>
        /// 转子转速PV1
        /// </summary>
        public string Label_RotorSpeed_PV1
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_RotorSpeed_PV1"); }
        }
        /// <summary>
        /// 转子转速PV2
        /// </summary>
        public string Label_RotorSpeed_PV2
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_RotorSpeed_PV2"); }
        }
        /// <summary>
        /// 转子转速PV3
        /// </summary>
        public string Label_RotorSpeed_PV3
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_RotorSpeed_PV3"); }
        }
        #endregion 转子转速

        #region 氩气流量
        /// <summary>
        /// 氩气流量PV1
        /// </summary>
        public string Label_ArgonFlow_PV1
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_ArgonFlow_PV1"); }
        }
        /// <summary>
        /// 氩气流量PV2
        /// </summary>
        public string Label_ArgonFlow_PV2
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_ArgonFlow_PV2"); }
        }
        /// <summary>
        /// 氩气流量PV3
        /// </summary>
        public string Label_ArgonFlow_PV3
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_ArgonFlow_PV3"); }
        }
        #endregion 氩气流量

        #region 氯气流量
        /// <summary>
        /// 氯气流量PV1
        /// </summary>
        public string Label_ChlorineFlow_PV1
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_ChlorineFlow_PV1"); }
        }
        /// <summary>
        /// 氯气流量PV2
        /// </summary>
        public string Label_ChlorineFlow_PV2
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_ChlorineFlow_PV2"); }
        }
        /// <summary>
        /// 氯气流量PV3
        /// </summary>
        public string Label_ChlorineFlow_PV3
        {
            get { return TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Label_ChlorineFlow_PV3"); }
        }

        #endregion 氯气流量
        #endregion 页面属性

        #region 定义页面执行命令
        /// <summary>
        /// 显示PV编辑框
        /// </summary>
        public DelegateCommand EditPVCommand { get; set; }
        #endregion 定义页面执行命令

        #region 线程对象
        /// <summary>
        /// 标签值变化监听线程
        /// </summary>
        private Thread tagInfoSyncThread = null;
        #endregion 线程对象

        #region 方法
        /// <summary>
        /// 构造函数
        /// </summary>
        public DegassingTankPageViewModel()
        {
            // 初始化命令
            initCommand();
            // 初始化线程
            initThread();
            // 消息通道注册
            initMessenger();
            // 初始化基础信息
            initBaseSet();
        }

        /// <summary>
        /// 初始化基础信息
        /// </summary>
        private void initBaseSet()
        {
            // 页面初始化
            HashSet<Control> tempHashSet = SysCommon.getCurrentControl(showPageTitle);
            if (tempHashSet.Count > 0)
            {
                DoChanged(tempHashSet);
            }
            /** 设定值初始化 **/
            _Button_Temperature_SP = TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Button_Temperature_SP");
            RaisePropertyChanged("Button_Temperature_SP");
        }

        /// <summary>
        /// 消息通道注册
        /// ViewModel之间数据通信使用，整个项目全局唯一
        /// </summary>
        private void initMessenger()
        {
            //获取CV值
            Messenger.Default.Register<string>(this, showPageTitle + "Button_Temperature_SP", (message) =>
            {
                Button_Temperature_SP = message;
            });
            //页面切换执行控件刷新
            Messenger.Default.Register<string>(this, showPageTitle + "_DoChange", (message) =>
            {
                HashSet<Control> tempHashSet = SysCommon.getCurrentControl(showPageTitle);
                if (tempHashSet.Count > 0)
                {
                    DoChanged(tempHashSet);
                }
            });
        }

        /// <summary>
        /// 初始化线程
        /// </summary>
        private void initThread()
        {
            tagInfoSyncThread = new Thread(syncThreadFunc);
            tagInfoSyncThread.SetApartmentState(ApartmentState.STA);
            tagInfoSyncThread.IsBackground = true;
            tagInfoSyncThread.Start();
        }

        /// <summary>
        /// 初始化命令
        /// </summary>
        private void initCommand()
        {
            // 显示PV编辑框
            EditPVCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(editPVHandler)
            };
        }

        /// <summary>
        /// 显示PV编辑框
        /// </summary>
        /// <param name="obj"></param>
        private void editPVHandler(object obj)
        {
            string param = obj.ToString();
            WindowManager.ShowModalDigitalInputPanel("SetCVPanel", showPageTitle + "Button_Temperature_SP", Button_Temperature_SP);
        }

        /// <summary>
        /// 同步标签数据方法
        /// </summary>
        private void syncThreadFunc()
        {
            // 创建消费者对象
            IConsumer<string, object> KafkaConsumer = KafkaHelper.GetConsumer();
            while (true)
            {
                // 网络中断不进行消费监听
                if (SystemConfig.SystemStatus == "网络中断")
                {
                    Thread.Sleep(1000);
                    continue;
                }
                if (ThreadController.RoundBarHoldingFurnaceThreadState)
                {
                    //开始消费
                    var result = KafkaConsumer.Consume();
                    if (result != null)
                    {
                        //手动提交
                        KafkaConsumer.Commit(result);
                        //获取消息内容
                        string tagId = result.Message.Value.ToString();
                        HashSet<Control> hashSet = KafkaHelper.UpdateCurrentUserTagValue(Int32.Parse(tagId), KafkaHelper.GetTagValueByKey(tagId.ToString()), showPageTitle);
                        if (hashSet != null)
                        {
                            DoChanged(hashSet);
                        }
                    }
                }
                Thread.Sleep(10);
            }
        }

        /// <summary>
        /// 执行动作
        /// </summary>
        /// <param name="hashSet"></param>
        private void DoChanged(HashSet<Control> hashSet)
        {
            //遍历返回的控件信息刷新对应的控件
            foreach (Control item in hashSet)
            {
                RaisePropertyChanged(item.ControlName);
            }
        }
        #endregion 方法
    }
}
